package cn.summit;

/**
 *
 * 合并两个有序链表
 * @author summit
 * @since 2021/8/16 13:48
 */
public class 合并两个有序链表 {
	public static void main(String[] args) {
		ListNode l1 = new ListNode(1);
		l1.next = new ListNode(2);
		l1.next.next = new ListNode(3);

		ListNode l2 = new ListNode(1);
		l2.next = new ListNode(3);
		l2.next.next = new ListNode(4);
		ListNode listNode = mergerListNode(l1, l2);
		System.out.println(listNode);
	}

	public static ListNode mergerListNode(ListNode l1, ListNode l2) {
		ListNode node = new ListNode(0);
		ListNode current = node;
		while (l1 != null && l2 != null) {
			if (l1.val < l2.val) {
				current.next = l1;
				l1 = l1.next;
			} else {
				current.next = l2;
				l2 = l2.next;
			}
			current = current.next;
		}
		// 补全
		current.next = l1 != null ? l1 : l2;
		return node.next;
	}

	static class ListNode {
		int val;

		ListNode next;

		public int getVal() {
			return val;
		}

		public void setVal(int val) {
			this.val = val;
		}

		public ListNode getNext() {
			return next;
		}

		public ListNode setNext(int val) {
			this.next = new ListNode(val);
			return this.next;
		}

		ListNode() {
		}

		ListNode(int val) {
			this.val = val;
		}

		ListNode(int val, ListNode next) {
			this.val = val;
			this.next = next;
		}
	}
}
